package com.personal.website.lottery.spider;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 10171225 on 2016/2/15.
 */
public class Spider {

    private SpiderProperties spiderProperties;
    private DbAccessor dbAccessor;
    private String html;
    public Spider(){
        spiderProperties = new SpiderProperties();
        dbAccessor = new DbAccessor(spiderProperties);
        html = Util.spideHtml(spiderProperties);
    }

    public SpiderProperties getSpiderProperties() {
        return spiderProperties;
    }

    public void execute(){
        String querySql = constructQuerySql();
        String insertSql = constructInsertSql();
        if(!dbAccessor.exists(querySql)){
            dbAccessor.execute(insertSql);
        }
    }

    public String constructQuerySql(){
        return String.format("select * from double_color_ball where issue = '%s' limit 10",parseIssue());
    }

    public String constructInsertSql(){
        String issue = parseIssue();
        List<String> redballs = parseRedBalls();
        String red_one = redballs.get(0);
        String red_two = redballs.get(1);
        String red_three = redballs.get(2);
        String red_four = redballs.get(3);
        String red_five = redballs.get(4);
        String red_six = redballs.get(5);
        String blue = parseBlueBall();
        String draw_date = parseDrawDate();
        String currentPrizePool = parseCurrentPrizePool();
        String prizeInfo = parsePrizeRecords();

        return String.format("insert into double_color_ball (issue,red_one,red_two,red_three,red_four,red_five,red_six,blue,draw_date,current_prize_pool,prize_info)  values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                issue,red_one,red_two,red_three,red_four,red_five,red_six,blue,draw_date,currentPrizePool,prizeInfo);
    }


    public String parsePrizeRecords(){
        List<String> records = new ArrayList<String>();
        for(String regex : spiderProperties.getPrizeRecordsRegex()){
            records.addAll(parseValue(regex));
        }
        String prizeInfo = "";
        for (String record : records){
            prizeInfo += record + "$";
        }
        return prizeInfo.substring(0,prizeInfo.length() - 1);
    }

    public String parseIssue(){
        List<String> lst = parseValue(spiderProperties.getIssueRegex());
        if(lst.size() != 0){
            return lst.get(0);
        }
        return "";
    }

    public String parseDrawDate(){
        List<String> lst = parseValue(spiderProperties.getDrawDateRegex());
        if(lst.size() != 0){
            return lst.get(0);
        }
        return "";
    }

    public String parseCurrentPrizePool(){
        List<String> lst = parseValue(spiderProperties.getCurrentPrizePoolRegex());
        if(lst.size() != 0){
            return lst.get(0);
        }
        return "";
    }

    public List<String> parseRedBalls(){
        List<String> redBalls = new ArrayList<String>();
        for(String regex:spiderProperties.getRedBallsRegex()){
            redBalls.addAll(parseValue(regex));
        }
        return redBalls;
    }

    public String parseBlueBall(){
        List<String> lst = parseValue(spiderProperties.getBlueBallRegex());
        if(lst.size() != 0){
            return lst.get(0);
        }
        return "";
    }

    public List<String> parseValue(String regex){
        Pattern p=Pattern.compile(regex);
        Matcher m=p.matcher(html);
        List<String> lst = new ArrayList<String>();
        while(m.find()){
            String record = "";
            for(int i = 1;i <= m.groupCount();i ++){
                record += m.group(i).trim() + ";";
            }
            lst.add(record.substring(0,record.length() - 1));
        }
        return lst;
    }

}
